<?php 
/**
 * This file is part of DomAr.
 * 
 * DomAr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomAr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomAr.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* @package DomAr
* @subpackage Property
*/
abstract class DomArProperty implements IDomArProperty {
	
	/**
	* @param String Property name
	*/
	protected $name;
	
	public function __construct( $name ) {
		$this->name = $name;
	}
	
	final public function __get( $var ) {
		return $this->$var;
	}
	
	final public function __toString() {
		return $this->name;
	}

}

/**
* Interfaces that DomArProperties might use to get access to different listening actions.
* @package DomAr
* @subpackage Property

*/	
interface IDomArProperty {
	
}

/**
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertyAlterTableListener extends IDomArProperty {
	
	/**
	* @return Boolean true if table was changed
	* This method will be called when creating or updating DomArObject table.
	*/
	public function alterTable( DomArTable $table );

}

/**
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertyInitListener extends IDomArProperty  {
	
	/**
	* @param DomArObject $object Created object
	* @param Array $value All values from database.
	* This method will be called when loading DomArObject from database.
	* Should return value that will be omitted to object.
	* @return Mixed
	*/
	public function init( DomArObject $object, array &$values );
	
}

/**
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertyLoadListener extends IDomArPropertyInitListener {
	
	/**
	* @param DomArObject $object
	* @param Mixed $propertyValue
	* This method is will be called when user is tring to access lazy property and its value is not loaded. This method should load the value
	* @return Mixed Loaded value
	*/
	public function load( DomArObject $object, &$propertyValue );
	
	/**
	* @param DomArObject $object
	* @param Mixed $propertyValue
	* Checks if property value is loaded or not
	* @return Boolean
	*/
	public function isLoaded( DomArObject $object, &$propertyValue );
	
}

/**
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertyCompareListener extends IDomArProperty {
	
	/**
	* @return Boolean true if values are equal, false if not.
	* This method is called before setting and validating property value. If values are same, then no validation or change will occur.
	*/
	public function compare( DomArObject $object, &$oldPropertyValue, &$newPropertyValue );

}
/**
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertyBeforeSetListener extends IDomArProperty  {
	
	/**
	* @param DomArObject $object
	* @param Mixed $oldPropertyValue
	* @param Mixed $newPropertyValue
	* @return Mixed Changed value
	* This method will be called when object property is updated by user. ($item->value = "new value").
	* Should prepare the value. Value is already validated, so this method should not validate $newPropertyValue
	*/
	public function beforeSet( DomArObject $object, &$oldPropertyValue, &$newPropertyValue );
	
}

/**
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertyAfterSetListener extends IDomArProperty  {
	
	/**
	* @param DomArObject $object
	* @param Mixed $propertyValue
	* @return Mixed Changed value
	*/
	public function afterSet( DomArObject $object, &$propertyValue );
	
}

/**
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertyValidator extends IDomArProperty  {

	/**
	* @param DomArObject $object
	* @param Mixed $propertyValue
	* @param Array $customParams This can be used to send custom values to error message
	* Valides $propertyValue
	* @return Boolean True if $propertyValue can be omitted to object.
	*/
	public function validate( DomArObject $object, &$propertyValue, array &$customParams = array());
	
}


/**
* This listener will be used before insert query is sent to database.
*  
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertyBeforeInsertListener extends IDomArProperty  {

	
	/**
	* Will be invoked before insert query is sent to database. 
	*
	* Should update $fieldNames and $fieldValues 
	* $fieldNames holds mysql column field names. If you add new field always add comma first!
	* $fieldValues holds mysql column field values. If you add new value always add comma first!
	* Data added to these variables will not be checked. So be careful what you omit to them. (For example if addslashes is not used you may create a security problem)
	* These variables will be inserted directly into insert query.
	*
	* @return void
	*/
	public function beforeInsert( DomArSession $session, DomArObject $object, &$propertyValue );
	
}

/**
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertyInsertListener extends IDomArProperty  {

	
	/**
	* @return void
	*/
	public function insert( DomArObject $object, &$propertyValue, DomArValueSet $set  );
	
}

/**
* This listener will be used after insert query is sent to database. Object has id when it is invoked.
*  
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertyAfterInsertListener extends IDomArProperty  {

	
	/**
	* @return Array Column names that property will use.
	*/
	public function afterInsert( DomArObject $object, &$propertyValue );
	
	
}

/**
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertyBeforeUpdateListener extends IDomArProperty  {
	
	/**
	* @param DomArObject $object
	* @param Mixed $propertyValue
	* @return String
	*/
	public function beforeUpdate( DomArSession $session, DomArObject $object, &$propertyValue );

}

/**
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertyUpdateListener extends IDomArProperty  {
	
	/**
	* @param DomArObject $object
	* @param Mixed $propertyValue
	* @return String
	*/
	public function update( DomArObject $object, &$propertyValue, DomArValueSet $set );

}

/**
* @package DomAr
* @subpackage Property
*/
interface IDomArPropertyAfterUpdateListener extends IDomArProperty  {
	
	/**
	* @param DomArObject $object
	* @param Mixed $propertyValue
	*/
	public function afterUpdate( DomArObject $object, &$propertyValue );
	
}


/**
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertyBeforeDeleteListener extends IDomArProperty  {
	
	/**
	* @param DomArObject $object
	* @param Mixed $propertyValue
	* This method will be called when object is beeing deleted.
	*/
	public function beforeDelete( DomArObject $object, &$propertyValue );
	
}

/**
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertyAfterDeleteListener extends IDomArProperty  {
	
	/**
	* @param DomArObject $object
	* @param Mixed $propertyValue
	* This method will be called when object is beeing deleted.
	*/
	public function afterDelete( DomArObject $object, &$propertyValue );
	
}

/**
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertyCloneListener extends IDomArProperty  {
	
	/**
	* @param DomArObject $object
	* @param Mixed $propertyValue
	* This method will be called when object is beeing cloned
	*/
	public function _clone( DomArObject $object, &$propertyValue );
	
}

/**
* @package DomAr
* @subpackage Property
*/	
interface IDomArPropertySerializeListener extends IDomArProperty  {
	
	/**
	* @param DomArObject $object
	* @param Mixed $propertyValue
	* This method will be called when object serialize method is invoked (DO NOT use PHP serialize() function as this will not invoke this! Use $object->serialize())
	*/
	public function serialize( DomArObject $object, $propertyValue );
	
	/**
	* @param Mixed $propertyValue
	* This method will be called when object is unserialized (Use DomArObject::unserialize(String $serialized_domar_object) )
	* @param DomArObject $object
	*/
	public function unserialize( DomArObject $object, $propertyValue );
	
}

?>